package com.wei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wei.entity.Chapter;
import com.wei.entity.Video;
import com.wei.entity.vo.ChapterVo;
import com.wei.entity.vo.VideoVo;
import com.wei.service.ChapterService;
import com.wei.mapper.ChapterMapper;
import com.wei.service.VideoService;
import com.wei.service_base.exception.XueYuanException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author HASEE
 * @description 针对表【edu_chapter(课程)】的数据库操作Service实现
 * @createDate 2022-11-23 09:57:54
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter>
        implements ChapterService {

    @Autowired
    private VideoService videoService;

    @Override
    public List<ChapterVo> getChapterVideoByCid(String courseId) {

        //根据课程id获取所有章节信息
        LambdaQueryWrapper<Chapter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Chapter::getCourseId, courseId).orderByAsc(Chapter::getGmtCreate);

        List<Chapter> chapterList = baseMapper.selectList(queryWrapper);

        //获取所有视频信息
        LambdaQueryWrapper<Video> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Video::getCourseId, courseId).orderByAsc(Video::getGmtCreate);

        List<Video> videoList = videoService.list(queryWrapper1);

        //封装最终结果
        List<ChapterVo> list = new ArrayList<>();

        for (Chapter chapter : chapterList) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter, chapterVo);

            //封装videoVo
            List<VideoVo> videoVos = new ArrayList<>();

            for (Video video : videoList) {
                if (video.getChapterId().equals(chapterVo.getId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video, videoVo);

                    videoVos.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVos);

            list.add(chapterVo);
        }

        List<ChapterVo> collect = list.stream()
                .sorted((Comparator.comparing(ChapterVo::getSort)))
                .map(chapterVo -> {
                    List<VideoVo> videoVos = chapterVo.getChildren().stream()
                            .sorted(Comparator.comparing(VideoVo::getSort))
                            .collect(Collectors.toList());
                    chapterVo.setChildren(videoVos);
                    return chapterVo;
                })
                .collect(Collectors.toList());

        return collect;
    }

    @Override
    public boolean deleteChapterById(String id) {

        LambdaQueryWrapper<Video> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Video::getChapterId, id);

        long count = videoService.count(queryWrapper);

        if (count == 0) {

            int i = baseMapper.deleteById(id);

            return i > 0;
        } else {
            throw new XueYuanException(20001, "不能删除~");
        }
    }
}




